﻿$(document).ready(Page_Load);

function Page_Load(e) {
    $('[name="btnSave"]').button();

    $('[name="btnSave"]').click(function () {
        ValidateInput();
    });
}

function ValidateInput() {
    var email = $("#new-email"),
            email2 = $("#new-email2"),
            password = $("#old-password"),
			password1 = $("#new-password1"),
            password2 = $("#new-password2"),
            question = $("#new-question"),
            answer = $("#new-answer"),
			allFields = $([]).add(email).add(email2).add(password).add(password1).add(password2).add(question).add(answer);

    var bValid = true;
    var bUpdate = false;

    if(email.val().length > 0) {
        bValid = bValid && checkLength(email, "email", 6);
        bValid = bValid && checkPasswords(email, email2, "Emails");
        bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Enter a valid email address.  eg. user@email.com");

        bUpdate = true;
    }

    if(password1.val().length > 0) {
        bValid = bValid && checkPasswords(password1, password2, "Passwords");
        bValid = bValid && checkRegexp(password1, /^.*(?=.{7,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/, "Password length must be greated than 7 and contain at least 1 lowercase, 1 uppercase, 1 number and 1 special character.");

        bUpdate = true;
    }
    
    if((question.val().length > 0 && answer.val().length > 0) || (question.val().length == 0 && answer.val().length == 0)) {
        if ((question.val().length > 0 && answer.val().length > 0)) {
            bValid = bValid && checkLength(question, "security question", 3);
            bValid = bValid && checkLength(answer, "security answer", 3);

            bUpdate = true;
        }
    } else {
        question.addClass("ui-state-error");
        answer.addClass("ui-state-error");
        updateErrors("Security question and answer are linked.  If one is changed both must be changed.");        
    }

    if (bValid && bUpdate) {
        var webMethodParams = JSON.stringify({ CurrentPassword: password.val(), Password: password1.val(), Email: email.val(), Question: question.val(), Answer: answer.val() });

        $.ajax(
        {
            type: "POST",
            async: true,
            url: "Settings.aspx/UpdateUser",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: webMethodParams,
            success: UpdateUser_Success,
            error: UpdateUser_Error
        });
    }

    if (!bUpdate && bValid) {
        updateErrors("No data was updated.");
    }

}


function UpdateUser_Success(data, textStatus, XMLHttpRequest) {
    var result = data.d;
    switch (result[0]) {
        case "Success":
            var email = $("#new-email"),
            email2 = $("#new-email2"),
            password = $("#old-password"),
			password1 = $("#new-password1"),
            password2 = $("#new-password2"),
            question = $("#new-question"),
            answer = $("#new-answer"),
            currentemail = $("#currentEmail"),
            currentquestion = $("#currentQuestion"),
			allFields = $([]).add(email).add(email2).add(password).add(password1).add(password2).add(question).add(answer);

            if (email.val().length > 0) {
                currentemail.html(email.val());
            }

            if (question.val().length > 0) {
                currentquestion.html(email.val());
            }

            allFields.val("");

            var $dialog = $('<div></div>')
		        .html('Your account information has successfully been updated.')
		        .dialog({
		            autoOpen: false,
		            resizable: false,
		            height: 150,
		            width: 300,
		            modal: true,
		            title: 'Account Update Successfully'
		        });
            $dialog.dialog('open');
            break;
        default:
            updateErrors(GetStatusMessage(result));
            break;
    }
}

function UpdateUser_Error(XMLHttpRequest, textStatus, errorThrown) {
    Log(XMLHttpRequest, "ValidateNewUser", 100);
    alert("An error occurred validating the login. Error: " + XMLHttpRequest);

}